package com.grape.action.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.grape.action.common.BaseAction;
import com.grape.domain.system.Menu;
import com.grape.domain.system.User;
import com.grape.service.system.MenuService;


@Namespace("/")
@Scope("prototype")
@ParentPackage("json-default")
@Controller
public class MenuAction extends BaseAction<Menu>{
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private MenuService ms;
	
	@Action(value="menu_page",results=@Result(name="success",type="json"))
	public String page() {
		Integer parseInt = Integer.parseInt(model.getPage());
		Pageable pageable = new PageRequest(parseInt-1, rows);
		
		Page<Menu> page = ms.findAll(pageable);
		
		
		pushPageDataToValueStack(page);
		return SUCCESS;
	}
	
	
	@Action(value="menu_list",results=@Result(name="success",type="json"))
	public String list() {
		List<Menu> list = ms.findAll();
		ServletActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
	
	@Action(value="menu_save",results=@Result(name="success",type="json"))
	public String save() {
		ms.save(model);
		
		Map<String,Object> mp = new HashMap<>();  
		mp.put("msg", "菜单保存成功");
		ServletActionContext.getContext().getValueStack().push(mp);
		return SUCCESS;
	}
	
	
	@Action(value="menu_user",results=@Result(name="success",type="json"))
	public String findByUser() {
		Subject subject = SecurityUtils.getSubject();
		User user = (User)subject.getPrincipal();
		
		List<Menu> list = ms.findByUser(user);
		ServletActionContext.getContext().getValueStack().push(list);
		
		return SUCCESS;
	}
	
	
}
